## -*-m4-*-

dnl Process this file with autoconf to produce a configure script.

# FILE:
# configure.in
#
# FUNCTION:
# implements checks for a variety of system-specific functions

AC_INIT(inc/libofx.h)
AM_CONFIG_HEADER(config.h)
AC_CONFIG_AUX_DIR(config)
AC_PROG_CC
AC_PROG_CXX
#AC_PROG_RANLIB

m4_include([libcurl.m4])

LIBOFX_MAJOR_VERSION=0
LIBOFX_MINOR_VERSION=8
LIBOFX_MICRO_VERSION=2
LIBOFX_VERSION=$LIBOFX_MAJOR_VERSION.$LIBOFX_MINOR_VERSION.$LIBOFX_MICRO_VERSION

AC_SUBST(LIBOFX_MAJOR_VERSION)
AC_SUBST(LIBOFX_MINOR_VERSION)
AC_SUBST(LIBOFX_MICRO_VERSION)
AC_SUBST(LIBOFX_VERSION)
AM_INIT_AUTOMAKE(libofx,$LIBOFX_VERSION)

LIBOFX_SO_CURRENT=3
LIBOFX_SO_REVISION=0
LIBOFX_SO_AGE=0
LIBOFX_SO_EFFECTIVE="`echo \$(($LIBOFX_SO_CURRENT-$LIBOFX_SO_AGE))`"
AC_SUBST(LIBOFX_SO_CURRENT)
AC_SUBST(LIBOFX_SO_REVISION)
AC_SUBST(LIBOFX_SO_AGE)

AM_MAINTAINER_MODE

AC_PROG_INSTALL

AC_LIBTOOL_DLOPEN
AM_PROG_LIBTOOL

AC_ISC_POSIX
AC_C_BIGENDIAN
AC_PROG_MAKE_SET
AC_HEADER_STDC

AC_ARG_WITH(opensp-includes, 
[  --with-opensp-includes=PATH     specify where to look for OpenSP includes  
                                  - default is /usr/include/OpenSP)],
			OPENSPINCLUDES="$with_opensp_includes",
			OPENSPINCLUDES="" )

AC_ARG_WITH(opensp-libs, 
[  --with-opensp-libs=PATH         specify where to look for libosp 
                                  - default is /usr/lib],
			OPENSPLIBPATH="$with_opensp_libs",
			OPENSPLIBPATH="/usr/lib")
			
			echo $OPENSPLIBPATH
for d in /usr/include/OpenSP /usr/local/include/OpenSP /usr/include/sp/generic /usr/local/include/sp/generic; do
    if test "x$OPENSPINCLUDES" = x; then
	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -I$d"
	AC_MSG_CHECKING(for ParserEventGenerator.h in $d)
	AC_TRY_CPP([#include <ParserEventGeneratorKit.h>],
	[ AC_MSG_RESULT(yes); OPENSPINCLUDES=$d ], [ AC_MSG_RESULT(no) ])
	CPPFLAGS="$save_CPPFLAGS"
    fi
done


##Detect if OpenSP was compiled with SP_MULTI_BYTE, and put the result in config.h
##

#if 0
##This test works for me, but breaks for most distro because the config.h installed isn't really the one that was used to compile OpenSP
## So --with-opensp-multibyte is now the default.
AC_DEFUN([CHECK_SP_MULTI_BYTE],
 [
AC_CACHE_VAL(ox_sp_multibyte,
   [
 for d in $OPENSPINCLUDES/config.h $OPENSPINCLUDES/../config.h $OPENSPINCLUDES/../include/config.h ; do
    if test "x$OPENSPCONFIG_H" = x; then
	save_CPPFLAGS="$CPPFLAGS"
	CPPFLAGS="$CPPFLAGS -DOPENSPCONFIG_H=\"$d\""
	AC_MSG_CHECKING(for OpenSP's config.h in $d)
	AC_TRY_CPP([#include OPENSPCONFIG_H],
	[ AC_MSG_RESULT(yes); OPENSPCONFIG_H=$d ], [ AC_MSG_RESULT(no) ])
	CPPFLAGS="$save_CPPFLAGS"
    fi
 done
 if test "x$OPENSPCONFIG_H" = x; then
	AC_MSG_ERROR([OpenSP's config.h not found])
 fi
AC_MSG_CHECKING(for if OpenSP's was compiled with SP_MULTI_BYTE)
    ox_sp_multibyte=`sed 's/^#.*SP_MULTI_BYTE[ \t]*\([01]\)/\1/;t;d' < \
            $OPENSPCONFIG_H`])
     if test x"$ox_sp_multibyte" != x ;then
         AC_DEFINE_UNQUOTED(SP_MULTI_BYTE, $ox_sp_multibyte, [SP_MULTI_BYTE value from when OpenSP was compiled])
		 AC_MSG_RESULT(yes)
     else
AC_MSG_RESULT(no)
    fi
 ])
#endif

 AC_ARG_WITH(no-opensp-multibyte, 	
[  --with-no-opensp-multibyte     Force libofx to compile with the assumption that OpenSP was NOT compiled with SP_MULTI_BYTE defined],
		,
			AC_DEFINE(SP_MULTI_BYTE, 1, [SP_MULTI_BYTE value from when OpenSP was compiled])	 )
			
##if test x"$SP_MULTI_BYTE" == x ;then
##CHECK_SP_MULTI_BYTE
##fi

ac_save_CPPFLAGS="$CPPFLAGS"
if test "x$OPENSPINCLUDES" != x ; then
  CPPFLAGS="-I$OPENSPINCLUDES $CPPFLAGS"
fi
AC_LANG_CPLUSPLUS 
AC_CHECK_HEADERS([ParserEventGeneratorKit.h SGMLApplication.h EventGenerator.h], 
				[] , 
				[ AC_MSG_ERROR([OpenSP includes not found]) ], 
				[] )
				
OPENSPLIBS="-L$OPENSPLIBPATH -losp"
ac_save_LIBS="$LIBS"
LIBS="$OPENSPLIBS $LIBS"

AC_MSG_CHECKING([for libosp])
##dnl This is code from the opensp documentation, I modified it a little
##dnl It is really just a link test rather than a run test, it does nothing
AC_LANG_CPLUSPLUS 
	AC_TRY_RUN([
		#include "ParserEventGeneratorKit.h"
		using namespace std;
		class OutlineApplication : public SGMLApplication {
		public:
		  OutlineApplication() { }
		  void startElement(const StartElementEvent &event) {
		  }
		  void endElement(const EndElementEvent &) { }

		};
		int main(int argc, char **argv)
		{
		  ParserEventGeneratorKit parserKit;
		  EventGenerator *egp = parserKit.makeEventGenerator(argc - 1, argv + 1);
		  OutlineApplication app;
		}
	],
	[AC_MSG_RESULT([yes])],
	[AC_MSG_RESULT([no])
	AC_MSG_ERROR([unable to link a test program, is OpenSP installed?])],
	[AC_MSG_RESULT([unknown, assumed OK])])
				
CPPFLAGS="$ac_save_CPPFLAGS"
LIBS="$ac_save_LIBS"		

 
# check for doxygen, mostly stolen from http://log4cpp.sourceforge.net/
# ----------------------------------------------------------------------------
AC_DEFUN([BB_ENABLE_DOXYGEN],
[
AC_ARG_ENABLE(doxygen, [  --enable-doxygen        enable documentation generation with doxygen (auto)])
AC_ARG_ENABLE(dot, [  --enable-dot            use 'dot' to generate graphs in doxygen (auto)])              
AC_ARG_ENABLE(html-docs, [  --enable-html-docs      enable HTML generation with doxygen (yes)], [], [ enable_html_docs=yes])              
AC_ARG_ENABLE(latex-docs, [  --enable-latex-docs     enable LaTeX documentation generation with doxygen (no)], [], [ enable_latex_docs=no])              
if test "x$enable_doxygen" = xno; then
        enable_doc=no
else 
        AC_PATH_PROG(DOXYGEN, doxygen, , $PATH)
        if test x$DOXYGEN = x; then
                if test "x$enable_doxygen" = xyes; then
                        AC_MSG_ERROR([could not find doxygen])
                fi
                enable_doc=no
        else
                enable_doc=yes
                AC_PATH_PROG(DOT, dot, , $PATH)
        fi
fi
AM_CONDITIONAL(DOC, test x$enable_doc = xyes)

if test x$DOT = x; then
        if test "x$enable_dot" = xyes; then
                AC_MSG_ERROR([could not find dot])
        fi
        enable_dot=no
else
        enable_dot=yes
fi
AM_CONDITIONAL(ENABLE_DOXYGEN, test x$enable_doc = xtrue)
AC_SUBST(enable_dot)
AC_SUBST(enable_html_docs)
AC_SUBST(enable_latex_docs)
])

# check for doxygen
# ----------------------------------------------------------------------------
BB_ENABLE_DOXYGEN

# Check if getopt_long is available
# ----------------------------------------------------------------------------
dnl Available from the GNU Autoconf Macro Archive at:
dnl http://www.gnu.org/software/ac-archive/htmldoc/adl_func_getopt_long.html
dnl
AC_DEFUN([adl_FUNC_GETOPT_LONG],
 [AC_PREREQ(2.49)dnl
  # clean out junk possibly left behind by a previous configuration
  rm -f lib/getopt.h
  # Check for getopt_long support
  AC_CHECK_HEADERS([getopt.h])
  AC_CHECK_FUNCS([getopt_long],,
   [# FreeBSD has a gnugetopt library for this
    AC_CHECK_LIB([gnugetopt],[getopt_long],[AC_DEFINE([HAVE_GETOPT_LONG])],
     [# use the GNU replacement
      AC_LIBOBJ(getopt)
      AC_LIBOBJ(getopt1)
      AC_CONFIG_LINKS([lib/getopt.h:lib/gnugetopt.h])])])])

dnl check for getopt in standard library
adl_FUNC_GETOPT_LONG
AM_CONDITIONAL(NO_GETOPTLONG, test "$ac_cv_func_getopt_long" = no  )

# gengetopt command line parser generation
AC_ARG_ENABLE(gengetopt,
        AS_HELP_STRING(--disable-gengetopt,Disable rebuilding of command line parser with gengetopt),
[case "${enableval}" in
  yes) gengetopt=yes ;;
  no)  gengetopt=no ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --disable-gengetopt]) ;;
esac],[gengetopt=yes])

if test x$gengetopt = xyes ; then
   AC_CHECK_PROG(have_gengetopt, gengetopt, yes, no)

   if test x$have_gengetopt = xno ; then
     AC_MSG_WARN([*** Not rebuilding command line parser as gengetopt is not found ***])
     gengetopt=no
   fi
fi

AM_CONDITIONAL(USE_GENGETOPT, test "x$gengetopt" = xyes)

# check for curl
# ----------------------------------------------------------------------------

LIBCURL_CHECK_CONFIG([yes],[7.9.7], [libcurl_available=yes],  [libcurl_available=no])
if  test "$libcurl_available" = no; then
	AC_MSG_WARN([libcurl is not available. ofxconnect (Direct connect samples) will NOT be built.])
fi

PKG_CHECK_MODULES(LIBXMLPP,libxml++-1.0 >= 1.0.1,
	[
	AC_DEFINE(HAVE_LIBXMLPP, 1, [Defined if libxml++ is available])
	have_libxmlpp=yes],
        [AC_MSG_WARN([libxml++ 1.0 is not available. ofxconnect (Direct connect samples) will NOT be built.])
	have_libxmlpp=no])

#PKG_CHECK_MODULES(QT,qt-mt >= 3.2,
#	[AC_DEFINE(HAVE_QT, 1, [Defined if Qt Gui is available])],
#        [AC_MSG_WARN([Qt is not available. Some experimental direct connect samples will not be fully functional.])])

build_ofxconnect=no
if test "$libcurl_available" = yes; then
	if test "$have_libxmlpp" = yes; then
		build_ofxconnect=yes
	fi
fi
AM_CONDITIONAL([BUILD_OFXCONNECT], [test "$build_ofxconnect" = yes])
AC_SUBST(ofxconnect)
AC_SUBST(OPENSPINCLUDES)
AC_SUBST(OPENSPLIBS)
AC_SUBST(LIBXMLPP_CFLAGS)
AC_SUBST(LIBXMLPP_LIBS)
AC_SUBST(QT_CFLAGS)
AC_SUBST(QT_CFLAGS)

LIBOFX_DTD_DIR='${datadir}/libofx/dtd'
AC_SUBST(LIBOFX_DTD_DIR)

	AC_CONFIG_FILES([Makefile])
AC_OUTPUT( 		
	   		libofx.spec
			libofx.pc
			lib/Makefile
			inc/Makefile
			dtd/Makefile
			doc/Makefile
			ofx2qif/Makefile
			ofxdump/Makefile
			ofxconnect/Makefile
			)
